package com.railwayservice.grabticket.web;

import com.railwayservice.application.vo.ResultMessage;
import com.railwayservice.grabticket.entity.KyfwUser;
import com.railwayservice.grabticket.entity.Passenger;
import com.railwayservice.grabticket.service.PassengerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 乘客模块请求控制器。
 *
 * @author lid
 * @date 2017.4.11
 */

@Api(value = "乘客模块请求控制器", description = "乘客模块的相关操作")
@Controller
@RequestMapping(value = "/passenger", method = {RequestMethod.GET,RequestMethod.POST})
public class PassengerController {

    private final Logger logger = LoggerFactory.getLogger(PassengerController.class);

    private PassengerService passengerService;

    @Autowired
    public void setPassengerService(PassengerService passengerService) {
        this.passengerService = passengerService;
    }

    /**
     * 获取用户乘车人信息(由python端调用)
     *
     * @param stringObjectMap
     * @return
     */
    @SuppressWarnings("unchecked")
    @ResponseBody
    @RequestMapping(value = "/getPassengers", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "获取用户乘车人信息(由python端调用)", value = "获取用户乘车人信息(由python端调用)")
    public ResultMessage getPassengers(@RequestBody Map<String, Object> stringObjectMap) {
        logger.info("获取用户乘车人信息：" + ((Map<String, Object>) stringObjectMap.get("data")).get("normal_passengers"));

        passengerService.getPassengers(stringObjectMap);
        return ResultMessage.newSuccess("获取用户乘车人信息成功！");

    }

    /**
     * 添加用户乘车人信息(由python端调用)
     *
     * @param stringObjectMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addPassenger", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "添加用户乘车人信息(由python端调用)", value = "添加用户乘车人信息(由python端调用)")
    public ResultMessage addPassenger(@RequestBody Map<String, Object> stringObjectMap) {
        logger.info("添加用户乘车人信息：" + stringObjectMap);

        Passenger passengers = passengerService.addPassenger(stringObjectMap);
        return ResultMessage.newSuccess("添加用户乘车人信息成功！");

    }

    /**
     * 刷新用户乘车人信息(由python端调用)
     *
     * @param stringObjectMap
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/freshPassengers", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "刷新用户乘车人信息(由python端调用)", value = "刷新用户乘车人信息(由python端调用)")
    public ResultMessage freshPassengers(@RequestBody Map<String, Object> stringObjectMap) {
        logger.info("刷新用户乘车人信息：" + stringObjectMap);
        List<Passenger> freshPassengers = passengerService.getPassengers(stringObjectMap);
        return ResultMessage.newSuccess("刷新用户乘车人信息成功！").setData(freshPassengers);

    }

    /**
     * 绑定12306账号(每个微信用户只有一个12306账号，如果绑定新账号，则将原账号绑定关系删除)(由python端调用)
     *
     * @param userId
     * @param userName
     * @param passWord
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/bindingKyfwByUser", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "(每个微信用户只有一个12306账号，如果绑定新账号，则将原账号绑定关系删除)", value = "绑定12306账号(由python端调用)")
    public ResultMessage bindingKyfwByUser(String userId, String userName, String passWord) {
        logger.info("绑定12306账号：用户ID:" + userId + ",12306账号:" + userId + ",密码:" + passWord);

        KyfwUser kyfwUser = passengerService.bindingKyfwByUser(userId, userName, passWord);
        return ResultMessage.newSuccess("绑定12306账号成功！").setData(kyfwUser);

    }

    /**
     * 获取12306账号及乘客信息
     *
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getKyfwByUser", method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(notes = "获取12306账号及乘客信息", value = "获取12306账号及乘客信息")
    public ResultMessage getKyfwByUser(String userId) {
        logger.info("获取12306账号及乘客信息：用户ID:");

        Map<String, Object> objectMap = passengerService.getKyfwByUser(userId);
        return ResultMessage.newSuccess("获取12306账号及乘客信息成功！").setData(objectMap);

    }
}	
